//package com.example.demo.configuration.security;
//
//import com.example.demo.entity.security.*;
//import com.example.demo.service.user.impl.UserServiceImpl;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.crypto.password.PasswordEncoder;
//
//import javax.annotation.Resource;
//
///**
// * @description:
// * @author: cj
// * @create: 09-02-08 09:28:49
// */
//@Configuration
//public class SecurityConfigCope extends WebSecurityConfigurerAdapter {
//
//
//    /**
//     * 登录成功的返回 JSON 格式的数据给前端（否则为 html）
//     */
//    @Resource
//    private LoginSuccessHandler loginSuccessHandler;
//    /**
//     * 登录失败 JSON 格式的数据给前端（否则为 html）
//     */
//    @Resource
//    private LoginFailureHandler loginFailureHandler;
//    /**
//     * 未登录 JSON 格式的数据给前端（否则为 html）
//     */
//    @Resource
//    private LoginEntryPoint loginEntryPoint;
//    /**
//     * 权限不足 JSON 格式的数据给前端（否则为 html）
//     */
//    @Resource
//    private RoleAccessDeniedHandler roleAccessDeniedHandler;
//    @Resource
//    private Md5PasswordEncoder passwordEncoder;
//    @Resource
//    private UserServiceImpl userService;
//    @Resource
//    private ObjectMapper objectMapper;
//
//    /**
//     * 自定义密码校验
//     *
//     * @return Md5PasswordEncoder
//     */
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new Md5PasswordEncoder();
//    }
//
//
//    @Override
//    public void configure(HttpSecurity http) throws Exception {
//        http
//                .authenticationProvider( authenticationProvider() )
//                .httpBasic()
//                //未登录时，进行json格式的提示，很喜欢这种写法，不用单独写一个又一个的类
////                .authenticationEntryPoint( (request, response, authException) -> {
////                    response.setContentType( "application/json;charset=utf-8" );
////                    response.setStatus( HttpServletResponse.SC_FORBIDDEN );
////                    PrintWriter out = response.getWriter();
////                    Map<String, Object> map = new HashMap<String, Object>();
////                    map.put( "code", 403 );
////                    map.put( "message", "未登录" );
////                    out.write( objectMapper.writeValueAsString( map ) );
////                    out.flush();
////                    out.close();
////                } )
//                .authenticationEntryPoint( loginEntryPoint )
//
//                .and()
//                .authorizeRequests()
//                .anyRequest().authenticated() //必须授权才能范围
////                .and()
////                .authorizeRequests()
////                .antMatchers( "/demo/demo1" ).hasAuthority( "test" )
////                .anyRequest().authenticated()
//                .and()
//                .formLogin() //使用自带的登录
//                .permitAll()
//                //登录失败，返回json
////                .failureHandler( (request, response, ex) -> {
////                    response.setContentType( "application/json;charset=utf-8" );
////                    response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
////                    PrintWriter out = response.getWriter();
////                    Map<String, Object> map = new HashMap<String, Object>();
////                    map.put( "code", 401 );
////                    if (ex instanceof UsernameNotFoundException || ex instanceof BadCredentialsException) {
////                        map.put( "message", "用户名或密码错误" );
////                    } else if (ex instanceof DisabledException) {
////                        map.put( "message", "账户被禁用" );
////                    } else {
////                        map.put( "message", "登录失败!" );
////                    }
////                    out.write( objectMapper.writeValueAsString( map ) );
////                    out.flush();
////                    out.close();
////                } )
//                .failureHandler( loginFailureHandler )
//                //登录成功，返回json
////                .successHandler( (request, response, authentication) -> {
////                    Map<String, Object> map = new HashMap<String, Object>();
////                    map.put( "code", 200 );
////                    map.put( "message", "登录成功" );
////                    map.put( "data", authentication );
////                    response.setContentType( "application/json;charset=utf-8" );
////                    PrintWriter out = response.getWriter();
////                    out.write( objectMapper.writeValueAsString( map ) );
////                    out.flush();
////                    out.close();
////                } )
//                .successHandler( loginSuccessHandler )
//                .and()
//                .exceptionHandling()
//                //没有权限，返回json
////                .accessDeniedHandler( (request, response, ex) -> {
////                    response.setContentType( "application/json;charset=utf-8" );
////                    response.setStatus( HttpServletResponse.SC_FORBIDDEN );
////                    PrintWriter out = response.getWriter();
////                    Map<String, Object> map = new HashMap<String, Object>();
////                    map.put( "code", 403 );
////                    map.put( "message", "权限不足" );
////                    out.write( objectMapper.writeValueAsString( map ) );
////                    out.flush();
////                    out.close();
////                } )
//                .accessDeniedHandler( roleAccessDeniedHandler )
//                .and()
//                .logout()
//                //退出成功，返回json
////                .logoutSuccessHandler( (request, response, authentication) -> {
////                    Map<String, Object> map = new HashMap<String, Object>();
////                    map.put( "code", 200 );
////                    map.put( "message", "退出成功" );
////                    map.put( "data", authentication );
////                    response.setContentType( "application/json;charset=utf-8" );
////                    PrintWriter out = response.getWriter();
////                    out.write( objectMapper.writeValueAsString( map ) );
////                    out.flush();
////                    out.close();
////                } )
//                .permitAll();
//        //开启跨域访问
//        http.cors().disable();
//        //开启模拟请求，比如API POST测试工具的测试，不开启时，API POST为报403错误
//        http.csrf().disable();
////        http
////                .authenticationProvider( authenticationProvider() ).
////                authorizeRequests()
////                //表示配置请求路径
////                .antMatchers( "/user/accountLogin" )
////                .permitAll() // 指定 URL 无需保护。
////                .anyRequest() // 其他请求
////                .authenticated() //需要认证
////                .and().httpBasic()
////                //未登录
////                .authenticationEntryPoint( loginEntryPoint )
////                .and()
////                //开启登录
////                .formLogin()
////                // 登录成功
////                .successHandler( loginSuccessHandler )
////                // 登录失败
////                .failureHandler( loginFailureHandler )
////                .permitAll();
////
////
////        http
////                //关闭csrf
////                .csrf().disable()
////                //不通过session中获取SecurityContext
////                .sessionManagement()
////                .sessionCreationPolicy( SessionCreationPolicy.STATELESS )
////        ;
//
////        http.formLogin()
////                .loginPage( "/user/accountLogin" )
////                .loginProcessingUrl( "/user/accountLogin" )
////                .usernameParameter( "name" );
//    }
//
//
//    @Bean
//    @Override
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        return super.authenticationManagerBean();
//    }
//
//    @Override
//    public void configure(WebSecurity web) {
//        //对于在header里面增加token等类似情况，放行所有OPTIONS请求。
//        web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
//    }
//
//    @Bean
//    public AuthenticationProvider authenticationProvider() {
//        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
//        //对默认的UserDetailsService进行覆盖
//        authenticationProvider.setUserDetailsService( userService );
//        authenticationProvider.setPasswordEncoder( passwordEncoder );
//        return authenticationProvider;
//    }
//}
